using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using PickGold.Charting.Utilities;

namespace PickGold.Charting.Design
{
	internal class KeywordsStringEditorForm : Form
	{
		private Button _buttonCancel;
		private Button _buttonEdit;
		private Button _buttonInsert;
		private Button _buttonOk;
		private string _classTypeName;
		private Container _components;
		private GroupBox _groupBoxString;
		private string _initialString;
		private Label _labelDescription;
		private int _maxYValueIndex;
		private string _propertyName;
		private RichTextBox _richTextBox;
		private int _selectedKeywordLength;
		private string _selectedKeywordName;
		private int _selectedKeywordStart;
		private bool _updating;
		internal ArrayList applicableKeywords;
		internal Utilities.KeywordsRegistry KeywordsRegistry;
		public string ResultString;

		public KeywordsStringEditorForm()
		{
			this._propertyName = string.Empty;
			this._classTypeName = string.Empty;
			this._initialString = string.Empty;
			this.ResultString = string.Empty;
			this._maxYValueIndex = 9;
			this._selectedKeywordName = string.Empty;
			this._selectedKeywordStart = -1;
			this._selectedKeywordLength = -1;
			this.InitializeComponent();
		}

		public KeywordsStringEditorForm(string initialString, string classTypeName, string propertyName, int maxYValueIndex)
		{
			this._propertyName = string.Empty;
			this._classTypeName = string.Empty;
			this._initialString = string.Empty;
			this.ResultString = string.Empty;
			this._maxYValueIndex = 9;
			this._selectedKeywordName = string.Empty;
			this._selectedKeywordStart = -1;
			this._selectedKeywordLength = -1;
			this.InitializeComponent();
			this._classTypeName = classTypeName;
			this._propertyName = propertyName;
			this._maxYValueIndex = maxYValueIndex;
			this._initialString = initialString;
			this.ResultString = initialString;
		}

		private void buttonEdit_Click(object sender, EventArgs e)
		{
			string keyword = this._richTextBox.Text.Substring(this._selectedKeywordStart, this._selectedKeywordLength);
			KeywordEditor editor = new KeywordEditor(this.applicableKeywords, keyword, this._maxYValueIndex);
			if (editor.ShowDialog() == DialogResult.OK)
			{
				int length = this._selectedKeywordStart;
				int num2 = this._selectedKeywordLength;
				this._richTextBox.Text = this._richTextBox.Text.Substring(0, length) + editor.Keyword + this._richTextBox.Text.Substring(length + num2);
				this._richTextBox.SelectionStart = length + editor.Keyword.Length;
			}
			this._richTextBox.Focus();
		}

		private void buttonInsert_Click(object sender, EventArgs e)
		{
			KeywordEditor editor = new KeywordEditor(this.applicableKeywords, string.Empty, this._maxYValueIndex);
			if (editor.ShowDialog() == DialogResult.OK)
			{
				if (this._selectedKeywordLength > 0)
				{
					this._richTextBox.SelectionStart += this._richTextBox.SelectionLength;
					this._richTextBox.SelectionLength = 0;
					this._richTextBox.SelectedText = " " + editor.Keyword;
				}
				else
				{
					this._richTextBox.SelectionLength = this._selectedKeywordLength;
					this._richTextBox.SelectedText = editor.Keyword;
				}
			}
			this._richTextBox.Focus();
		}

		private void buttonOk_Click(object sender, EventArgs e)
		{
			this.ResultString = this._richTextBox.Text;
			this.ResultString = this.ResultString.Replace("\r\n", @"\n");
			this.ResultString = this.ResultString.Replace("\n", @"\n");
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && (this._components != null))
			{
				this._components.Dispose();
			}
			base.Dispose(disposing);
		}

		private ArrayList GetApplicableKeywords()
		{
			ArrayList list = new ArrayList();
			if (((this.KeywordsRegistry == null) || (this._propertyName.Length <= 0)) || (this._classTypeName.Length <= 0))
			{
				return list;
			}
		Label_0106:
			foreach (KeywordInfo info in this.KeywordsRegistry.registeredKeywords)
			{
				bool flag = false;
				foreach (string str in info.AppliesToTypes.Split(new char[] { ',' }))
				{
					if (this._classTypeName == str.Trim())
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					foreach (string str2 in info.AppliesToProperties.Split(new char[] { ',' }))
					{
						if (this._propertyName == str2.Trim())
						{
							list.Add(info);
							goto Label_0106;
						}
					}
				}
			}
			return list;
		}

		private string GetColorHilightedRtfText(string originalText)
		{
			string str = originalText;
			string name = string.Empty;
			this._selectedKeywordStart = -1;
			this._selectedKeywordLength = 0;
			int selectionStart = this._richTextBox.SelectionStart;
			str = str.Replace(@"\n", "\r\n");
			int num2 = 0;
			for (int i = 0; (i < str.Length) && (i < selectionStart); i++)
			{
				if (str[i] == '\\')
				{
					num2++;
				}
			}
			selectionStart += num2;
			str = str.Replace(@"\", @"\\");
			foreach (KeywordInfo info in this.applicableKeywords)
			{
				foreach (string str3 in info.GetKeywords())
				{
					int startIndex = 0;
					string str4 = str3.Trim();
					if (str4.Length > 0)
					{
						while ((startIndex = str.IndexOf(str4, startIndex, StringComparison.Ordinal)) >= 0)
						{
							int length = str4.Length;
							if ((info.SupportsValueIndex && (str.Length > (startIndex + length))) && (str[startIndex + length] == 'Y'))
							{
								length++;
								if ((str.Length > (startIndex + length)) && char.IsDigit(str[startIndex + length]))
								{
									length++;
								}
							}
							if ((info.SupportsFormatting && (str.Length > (startIndex + length))) && (str[startIndex + length] == '{'))
							{
								length++;
								int num6 = str.IndexOf("}", startIndex + length, StringComparison.Ordinal);
								if (num6 >= 0)
								{
									length += ((num6 - startIndex) - length) + 1;
								}
							}
							bool flag = (selectionStart > startIndex) && (selectionStart <= (startIndex + length));
							string str5 = str.Substring(0, startIndex);
							string str6 = string.Empty + @"\cf1";
							if (flag)
							{
								name = info.Name;
								name = name + "__" + startIndex.ToString(CultureInfo.InvariantCulture);
								this._selectedKeywordStart = startIndex;
								this._selectedKeywordStart -= selectionStart - this._richTextBox.SelectionStart;
								this._selectedKeywordLength = length;
								str6 = str6 + @"\b";
							}
							str6 = (str6 + @"\ul" + "#_") + str.Substring(startIndex + 1, length - 1) + @"\cf0";
							if (flag)
							{
								str6 = str6 + @"\b0";
							}
							str6 = str6 + @"\ul0 ";
							str = str5 + str6 + str.Substring(startIndex + length);
							if (startIndex < selectionStart)
							{
								selectionStart += str6.Length - length;
							}
							startIndex += str6.Length;
						}
					}
				}
			}
			this._selectedKeywordName = name;
			if (this._selectedKeywordName.Length > 0)
			{
				this._buttonEdit.Enabled = true;
			}
			else
			{
				this._buttonEdit.Enabled = false;
			}
			return str.Replace("\r\n", @"\par ").Replace("\n", @"\par ").Replace(@"\n", @"\par ").Replace("{", @"\{").Replace("}", @"\}").Replace("#_", "#");
		}

		private string GetRtfText(string originalText)
		{
			string str = string.Empty;
			str = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\r\n";
			return ((str + @"{\colortbl ;\red0\green0\blue255;}\r\n" + @"\viewkind4\uc1\pard\f0\fs17 ") + this.GetColorHilightedRtfText(originalText) + @"\par\r\n}");
		}

		private void InitializeComponent()
		{
			this._richTextBox = new RichTextBox();
			this._groupBoxString = new GroupBox();
			this._buttonEdit = new Button();
			this._buttonInsert = new Button();
			this._buttonOk = new Button();
			this._buttonCancel = new Button();
			this._labelDescription = new Label();
			this._groupBoxString.SuspendLayout();
			base.SuspendLayout();
			this._richTextBox.Location = new Point(0x10, 0x18);
			this._richTextBox.Name = "richTextBox";
			this._richTextBox.Size = new Size(320, 0x48);
			this._richTextBox.TabIndex = 0;
			this._richTextBox.Text = "<replaced at runtime>";
			this._richTextBox.WordWrap = false;
			this._richTextBox.SelectionChanged += new EventHandler(this.richTextBox_SelectionChanged);
			this._richTextBox.KeyDown += new KeyEventHandler(this.richTextBox_KeyDown);
			this._richTextBox.KeyPress += new KeyPressEventHandler(this.richTextBox_KeyPress);
			this._richTextBox.TextChanged += new EventHandler(this.richTextBox_TextChanged);
			this._groupBoxString.Controls.Add(this._buttonEdit);
			this._groupBoxString.Controls.Add(this._buttonInsert);
			this._groupBoxString.Controls.Add(this._richTextBox);
			this._groupBoxString.Location = new Point(8, 0x40);
			this._groupBoxString.Name = "groupBoxString";
			this._groupBoxString.Size = new Size(520, 0x68);
			this._groupBoxString.TabIndex = 1;
			this._groupBoxString.TabStop = false;
			this._groupBoxString.Text = SR.LabelStringWithKeywords;
			this._buttonEdit.Enabled = false;
			this._buttonEdit.Location = new Point(0x160, 0x40);
			this._buttonEdit.Name = "buttonEdit";
			this._buttonEdit.Size = new Size(0x98, 0x17);
			this._buttonEdit.TabIndex = 2;
			this._buttonEdit.Text = SR.LabelEditKeyword;
			this._buttonEdit.Click += new EventHandler(this.buttonEdit_Click);
			this._buttonInsert.Location = new Point(0x160, 0x20);
			this._buttonInsert.Name = "buttonInsert";
			this._buttonInsert.Size = new Size(0x98, 0x17);
			this._buttonInsert.TabIndex = 1;
			this._buttonInsert.Text = SR.LabelInsertNewKeyword;
			this._buttonInsert.Click += new EventHandler(this.buttonInsert_Click);
			this._buttonOk.DialogResult = DialogResult.OK;
			this._buttonOk.Location = new Point(0xb0, 180);
			this._buttonOk.Name = "buttonOk";
			this._buttonOk.Size = new Size(0x4b, 0x17);
			this._buttonOk.TabIndex = 2;
			this._buttonOk.Text = SR.LabelButtonOk;
			this._buttonOk.Click += new EventHandler(this.buttonOk_Click);
			this._buttonCancel.DialogResult = DialogResult.Cancel;
			this._buttonCancel.Location = new Point(0x120, 180);
			this._buttonCancel.Name = "buttonCancel";
			this._buttonCancel.Size = new Size(0x4b, 0x17);
			this._buttonCancel.TabIndex = 3;
			this._buttonCancel.Text = SR.LabelButtonCancel;
			this._labelDescription.Location = new Point(8, 8);
			this._labelDescription.Name = "labelDescription";
			this._labelDescription.Size = new Size(0x210, 0x30);
			this._labelDescription.TabIndex = 0;
			this._labelDescription.Text = SR.DesciptionCustomLabelEditorTitle;
			this.AutoScaleBaseSize = new Size(5, 13);
			base.CancelButton = this._buttonCancel;
			base.ClientSize = new Size(0x21a, 0xd7);
			base.Controls.Add(this._labelDescription);
			base.Controls.Add(this._buttonCancel);
			base.Controls.Add(this._buttonOk);
			base.Controls.Add(this._groupBoxString);
			base.FormBorderStyle = FormBorderStyle.FixedDialog;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "KeywordsStringEditorForm";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = SR.LabelStringKeywordsEditor;
			base.Load += new EventHandler(this.KeywordsStringEditorForm_Load);
			this._groupBoxString.ResumeLayout(false);
			base.ResumeLayout(false);
		}

		private void KeywordsStringEditorForm_Load(object sender, EventArgs e)
		{
			this._labelDescription.Text = this._labelDescription.Text.Replace(@"\n", "\n");
			this.applicableKeywords = this.GetApplicableKeywords();
			if (this.applicableKeywords.Count == 0)
			{
				this._buttonInsert.Enabled = false;
				this._buttonEdit.Enabled = false;
			}
			this._richTextBox.Rtf = this.GetRtfText(this._initialString);
		}

		private void richTextBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (((e.KeyCode == System.Windows.Forms.Keys.Delete) && (this._selectedKeywordStart >= 0)) && (this._selectedKeywordLength > 0))
			{
				int num = this._selectedKeywordStart;
				string str = this._richTextBox.Text.Substring(0, this._selectedKeywordStart) + this._richTextBox.Text.Substring(this._selectedKeywordStart + this._selectedKeywordLength);
				this._richTextBox.Text = str;
				this._richTextBox.SelectionStart = num;
				e.Handled = true;
			}
		}

		private void richTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar == '{') && (this._richTextBox.SelectionColor == Color.Blue))
			{
				e.Handled = true;
				this._richTextBox.SelectedText = "{}";
				this._richTextBox.SelectionStart--;
			}
		}

		private void richTextBox_SelectionChanged(object sender, EventArgs e)
		{
			if (((Control.ModifierKeys & System.Windows.Forms.Keys.Shift) != System.Windows.Forms.Keys.Shift) && !this._updating)
			{
				this._updating = true;
				string str = this._selectedKeywordName;
				string rtfText = this.GetRtfText(this._richTextBox.Text);
				if (str != this._selectedKeywordName)
				{
					int selectionStart = this._richTextBox.SelectionStart;
					this._richTextBox.Rtf = rtfText;
					this._richTextBox.SelectionStart = selectionStart;
					this._richTextBox.SelectionLength = 0;
				}
				this._updating = false;
			}
		}

		private void richTextBox_TextChanged(object sender, EventArgs e)
		{
			if (!this._updating)
			{
				this._updating = true;
				int selectionStart = this._richTextBox.SelectionStart;
				int selectionLength = this._richTextBox.SelectionLength;
				this._richTextBox.Rtf = this.GetRtfText(this._richTextBox.Text);
				this._richTextBox.SelectionStart = selectionStart;
				this._richTextBox.SelectionLength = selectionLength;
				this._updating = false;
			}
		}
	}
}

